package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingPlanProjectDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingPlanProject;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingPlanProjectMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanProjectVo;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    NursingPlanMapper nursingPlanMapper;

    @Autowired
    NursingPlanProjectMapper nursingPlanProjectMapper;


    @Override
    public List<NursingPlanVo> list() {
        return nursingPlanMapper.list();
    }

    @Override
    public void save(NursingPlanDto plans) {
        NursingPlan bo = BeanUtil.toBean(plans, NursingPlan.class);
        Long save = nursingPlanMapper.save(bo);
        System.out.println(save);
        List <NursingPlanProject> projectPlans = BeanUtil.copyToList(plans.getProjectPlans(), NursingPlanProject.class);
        for (NursingPlanProject projectPlan : projectPlans) {
            projectPlan.setPlanId(bo.getId());
            nursingPlanProjectMapper.save(projectPlan);
        }
    }

    @Override
    public PageResponse<NursingPlanVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page page=nursingPlanMapper.selectByPage(name,status);
        PageResponse<NursingPlanVo> resout = PageResponse.of(page,NursingPlanVo.class);
        return resout;
    }

    @Override
    public NursingPlanVo selectById(Long id) {
        NursingPlanVo nursingPlan = nursingPlanMapper.selectById(id);
        NursingPlanProject projectPlan = new NursingPlanProject();
        projectPlan.setPlanId(id);
        List<NursingPlanProjectVo> projectPlans = nursingPlanProjectMapper.list(projectPlan);
        nursingPlan.setProjectPlans(projectPlans);
        return nursingPlan;
    }

//    @Transient
    @Override
    public void update(Long id, NursingPlanDto dto) {
        NursingPlan bo = BeanUtil.toBean(dto, NursingPlan.class);
        bo.setId(id);
        nursingPlanMapper.update(bo);
        List <NursingPlanProject> projectPlans = BeanUtil.copyToList(dto.getProjectPlans(), NursingPlanProject.class);
        nursingPlanProjectMapper.deleteByPlanId(id);
        for (NursingPlanProject projectPlan : projectPlans) {
            projectPlan.setPlanId(id);
            nursingPlanProjectMapper.save(projectPlan);
        }
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        NursingPlan bo = new NursingPlan();
        bo.setId(id);
        bo.setStatus(status);
        nursingPlanMapper.update(bo);
    }

    @Override
    public void delete(Long id) {
        nursingPlanProjectMapper.selectByPlanId(id);
        nursingPlanMapper.delete(id);
//        nursingPlanProjectMapper.deleteByPlanId(id);
    }
}
